package com.voucher.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.voucher.entity.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ceshi {

    public static void main(String[] args) throws Exception {
        // 定义要序列化的数据结构
        DataHolder param = getpApa();
        log.info(JSON.toJSONString(param));
        String fdUrl = "http://10.1.3.97/U9/RestServices/UFIDA.U9.Cust.MXBST.GLAPI.VoucherAPI.ICreateAndApproveVoucher.svc/Do";
        String responseBody = HttpUtil.post(fdUrl, JSON.toJSONString(param));
        long start = System.currentTimeMillis();
        if (log.isDebugEnabled()) {
            log.debug("U9凭证接口返回：" + responseBody);
            long end = System.currentTimeMillis();
            log.debug("U9凭证接口耗时：" + (end - start) + "ms");
        }

        U9VoucherResult result = JSONObject.parseObject(responseBody, U9VoucherResult.class);

        if (result.getD() == null || !result.getD().getIsSuccess()) {
            throw new Exception("生成凭证失败。U9返回信息：" + responseBody);
        }
    }

    public static DataHolder getpApa() {
        DataHolder dataStructure = new DataHolder();

        // 填充context部分数据
        Context context = new Context();
        context.setCultureName("zh-CN");
        context.setEntCode("001");
        context.setOrgCode("220");
        context.setUserCode("admin");
        dataStructure.setContext(context);
        // 填充data部分数据
        Data data = new Data();
        data.setImportType(1);
        data.setCreateDate("2024-10-31");
        data.setVoucherCategory("01");

        // 填充DescFlexField部分数据（单头）
        DescFlexField descFlexField = new DescFlexField();
        descFlexField.setPrivateDescSeg1("测试单头01");
        data.setDescFlexField(descFlexField);

        // 填充Entries部分数据
        Entry entry1 = new Entry();
        entry1.setSerialNo(10);
        entry1.setAbstracts("测试凭证10");
        entry1.setAccount("");

        // 填充Entry的DescFlexField部分数据
        DescFlexField entry1DescFlexField = new DescFlexField();
        entry1DescFlexField.setPrivateDescSeg1("测试01");
        entry1.setDescFlexField(entry1DescFlexField);

        // 填充Entry的Segment部分数据
        Segment entry1Segment = new Segment();
        entry1Segment.setSegment1("224107");
        entry1Segment.setSegment3("1021009");
        entry1.setSegment(entry1Segment);

        entry1.setAccountedCr(0);
        entry1.setAccountedDr(20);
        entry1.setEnteredCr(0);
        entry1.setEnteredDr(20);
        entry1.setCurrency("C001");

        Entry entry2 = new Entry();
        entry2.setSerialNo(30);
        entry2.setAbstracts("测试凭证20");
        entry2.setAccount("");

        // 填充Entry的DescFlexField部分数据
        DescFlexField entry2DescFlexField = new DescFlexField();
        entry2DescFlexField.setPrivateDescSeg1("测试行02");
        entry2.setDescFlexField(entry2DescFlexField);

        // 填充Entry的Segment部分数据
        Segment entry2Segment = new Segment();
        entry2Segment.setSegment1("100301");
        entry2.setSegment(entry2Segment);

        entry2.setAccountedCr(13);
        entry2.setAccountedDr(0);
        entry2.setEnteredCr(13);
        entry2.setEnteredDr(0);
        entry2.setCurrency("C001");

        data.getEntries().add(entry1);
        data.getEntries().add(entry2);

        // 填充CFVoucherItems部分数据
        CFVoucherItem cfVoucherItem = new CFVoucherItem();
        cfVoucherItem.setEntrySerialNo(30);
        cfVoucherItem.setSerialNumber(10);
        cfVoucherItem.setCashFlowItem("1010201");
        cfVoucherItem.setCashFlowItemAttr(1);
        cfVoucherItem.setLCMoney(13);
        cfVoucherItem.setOCMoney(13);
        cfVoucherItem.setCurrency("C001");

        data.getCFVoucherItems().add(cfVoucherItem);

        dataStructure.setData(data);

        // 使用fastjson进行序列化
        String jsonString = JSON.toJSONString(dataStructure);
        System.out.println(jsonString);
        return dataStructure;
    }

    public JSONObject TODAXIE(JSONObject jsonObject) {
        // 原始的JSON数据对应的JSONObject对象（这里手动创建示例）

        // 遍历JSONObject的键值对，将键的首字母大写后重新设置值
        JSONObject newJsonObject = new JSONObject();
        for (String key : jsonObject.keySet()) {
            String capitalizedKey = key.substring(0, 1).toUpperCase() + key.substring(1);
            newJsonObject.put(capitalizedKey, jsonObject.get(key));
        }

        // 将处理后的JSONObject转换为JSON字符串输出
        System.out.println(newJsonObject.toJSONString());
        return newJsonObject;
    }
}
